<?php
namespace Admin\Controller;
use Think\Controller;

class LoginController extends Controller {
	//登入页面
    public function login(){
    	$config = array('MAIL_FROM'=>'876902658@qq.com');
    	C($config); // 合并配置参数到全局配置
    //	C('MAIL_HOST','smtp.qq.com');
		$this->display();
    }

    //登陆验证
	public function runlogin(){
		if (!IS_AJAX){
			$this->error("提交方式错误！",0,0);
		}else{
			$admin_username=I('admin_username');
			$admin_pwd=md5(I('admin_pwd'));
			
			$admin=M('admin')->where(array('admin_username'=>$admin_username,'admin_pwd'=>$admin_pwd))->find();
				if (!$admin||$admin_pwd!==$admin['admin_pwd']){
					$this->error('用户名或者密码错误，重新输入',0,0);
				}else{
					cookie('aid',$admin['admin_id'],86400); // 指定cookie保存时间
					cookie('admin_username',$admin['admin_username'],86400); // 指定cookie保存时间
					$this->success('恭喜您，登陆成功',1,1);
				}
		}
	}
	
	//注册验证
	public function reg(){
		$admin=M('admin');
		$vername=$admin->where(array('admin_username'=>I('admin_username')))->find();
		if ($vername){
			$this->error('用户已存在，请重新输入',0,0);
		}else{		
			$info=array(
				'admin_email'=>I('admin_email'),
				'admin_username'=>I('admin_username'),
				'admin_pwd'=>md5(I('admin_username')),
				'admin_addtime'=>time(),
			);
			$admin->add($info);
			$this->success('恭喜您，注册成功',1,1);
		}
	
	}
    
	public function logout(){
		session('aid',null);
		$this->redirect('Login/login');
	}
	
	//测试邮件发送
	public function runemail(){
		$admin=M('admin')->where(array('admin_email'=>I('email')))->find();
		if(!$admin){
			$this->error('邮件不存在，请重新输入',0,0);
		}
		$content="尊敬的用户，您好：<br>您当前的操作为找回密码，请点击以下链接重新设置密码<br><a href=http://127.0.0.1/newadmin/index.php/Admin/Login/checkpwd/email/$admin[admin_email].html>http://127.0.0.1/newadmin/index.php/Admin/Login/checkpwd/email/$admin[admin_email].html</a>";
		if(SendMail($_POST['email'],'找回密码服务',$content))
			$this->success('邮件发送成功！，打开邮件重新设置密码',1,1);
		else
			$this->error('邮件发送失败',0,0);
	}
	
	public function checkpwd(){
		$admin_email=I('email');
		$this->assign('admin_email',$admin_email);
		$this->display();
	}

	public function runcheckpwd(){
		$admin_email=I('admin_email');
		$admin_pwd=I('admin_pwd','','md5');
		$admin=M('admin')->where(array('admin_email'=>$admin_email))->setField('admin_pwd',$admin_pwd);
		$this->success('恭喜您，密码修改成功',U('login'),1);
	}


}